<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\product;
use app\api\controller\HomeCommon;
use app\api\model\TradeArea;
use app\Validate\PageMustValidate;
use app\Validate\SortTypeValidate;
use think\Controller;
use think\Request;

class TradeHall extends Controller
{
    /**
     * 查询交易大厅的商品 (库存判断的回头再做)
     * sortType 排序方式 1 是按销量排 2是按 上架时间排 3是按价格升序排 4是按价格降序排
     * product_name 产品名称
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     * @throws \think\exception\DbException
     */
    public function TradeList(Request $request){
        (new SortTypeValidate())->goCheck();
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where=[];
        $where[] = ['a.id','gt',0];
        $where[] = ['a.is_publish','eq',2];
        $where[] = ['b.is_publish','eq',2];
        if(!empty($post['product_name'])){ //如果有商品名称搜索 就是搜索商品名称和 占位码
            $where[] = ['b.product_name|a.position_code','like',"%".$post['product_name']."%"];
        }
        $sort ="a.id desc";
        $field = "a.id as hall_id,a.*,b.cover_img,b.product_name,b.sales_actual_number,c.price,c.min_number";
        switch ($post['sortType']){
            case 1:
                $sort = "b.sales_actual_number desc,a.id desc";
                break;
            case 2:
                $sort = "a.create_time desc,a.id desc";
                break;
            case 3:
                $sort = "c.price desc,a.id desc";
                break;
            case 4:
                $sort = "c.price,a.id desc";
                break;
        }

        $dataList = (new \app\api\model\TradeHall())
            ->alias("a")
            ->join("om_product b","a.product_id = b.id","left")
            ->join("om_product_price c","a.product_id = c.product_id and c.type = 2 and c.delete_time is null","left")
            ->field($field)
            ->where($where)
            ->order($sort)
            ->paginate($post['page_number'], false, ['page' => $post['page']]);

        $dataList=$dataList->toArray();
        $data = $dataList['data'];
        foreach ($data as $key=>$value){
            $data[$key]['cover_img'] = imgUrlToAbsolute($value['cover_img']);
        }
        $returnList = [
            'count'=>$dataList['total'],
            'dataList'=>$data
        ];
        return writeJson(200,$returnList);

    }

    /**
     * 交易大厅区域列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function TradeAreaList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where =[];
        $where['page'] = $post['page'];
        $data  = (new TradeArea())->wechatListTotal($where,"id,area_name","area_name",$post['page_number']);
        return writeJson(200,$data);
    }

    /**
     * 交易大厅区域下的产品
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function TradeAreaProduct(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where=[];
        $where[] = ['a.id','gt',0];
        $where[] = ['a.is_publish','eq',2];
        $where[] = ['b.is_publish','eq',2];
        $where = addWhere($post,['area_id'=>''],$where,1,'a.');//区域id
        if(isset($post['product_name'])){ //如果有商品名称搜索 就是搜索商品名称和 占位码
            $where[] = ['b.product_name|a.position_code','like',"%".$post['product_name']."%"];
        }
        //$sort ="a.line,a.row";
        $sort ="a.position_code,a.id desc";
        $field = "a.product_id,a.position_code,b.cover_img,a.id as hall_id";
        if($post['page']>0){
            //        $where = addWhere($post,['product_name'=>''],$where,2,"b.");//商品名称
            $dataList = (new \app\api\model\TradeHall())
                ->alias("a")
                ->join("om_product b","a.product_id = b.id","left")
                ->field($field)
                ->where($where)
                ->order($sort)
                ->paginate($post['page_number'], false, [
                    'page' => $post['page']]);
            $dataList=$dataList->toArray();
            $data = $dataList['data'];
            foreach ($data as $key=>$value){
                $data[$key]['cover_img'] = imgUrlToAbsolute($value['cover_img']);
            }
        }else{
            $dataList = (new \app\api\model\TradeHall())
                ->alias("a")
                ->join("om_product b","a.product_id = b.id","left")
                ->field($field)
                ->where($where)
                ->order($sort)
                ->select();
            $dataList=$dataList->toArray();
            $data = $dataList;
            $dataList['total'] = count($dataList);
            foreach ($data as $key=>$value){
                $data[$key]['cover_img'] = imgUrlToAbsolute($value['cover_img']);
            }
        }
        $returnList = [
            'count'=>$dataList['total'],
            'dataList'=>$data
        ];
        return writeJson(200,$returnList);
    }
}